package com.wally.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

public class ResponseUtil {
	private static final String STATUTS = "status";
	private static final String MSG = "msg";
	private static final String ERRORCODE = "errorCode";
	private static final String RESULT = "result";
	private static final String TOTAL = "total";
	private static final String ROWS = "rows";

	public static void validateResult(HttpServletResponse response, boolean status) {
		response.reset();
		response.setContentType("text/html;charset=UTF-8");
		try {
			response.getWriter().print(status);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/*
	 * public static void jsonResult(HttpServletResponse response, Object
	 * result) { response.reset();
	 * response.setContentType("application/json;charset=UTF-8"); try { String
	 * json = JSON.toJSONString(result); response.getWriter().print(json); }
	 * catch (IOException e) { e.printStackTrace(); } }
	 */

	public static Map<String, Object> tableResult(long total, List<?> dataList) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(TOTAL, Long.valueOf(total));
		map.put(ROWS, dataList);
		return map;
	}

	public static Map<String, Object> trueResult() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(STATUTS, Boolean.TRUE);
		return map;
	}

	public static Map<String, Object> falseResult() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(STATUTS, Boolean.FALSE);
		return map;
	}

	public static Map<String, Object> trueTipResult(String msg) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(STATUTS, Boolean.TRUE);
		map.put(MSG, msg);
		return map;
	}

	public static Map<String, Object> falseTipResult(String msg) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(STATUTS, Boolean.FALSE);
		map.put(MSG, msg);
		return map;
	}

	public static Map<String, Object> trueBizResult(Object result) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(STATUTS, Boolean.TRUE);
		map.put(RESULT, result);
		return map;
	}

	public static Map<String, Object> falseBizResult(Object result) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(STATUTS, Boolean.FALSE);
		map.put(RESULT, result);
		return map;
	}

	public static Map<String, Object> trueBizResult(Object result, String msg) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(STATUTS, Boolean.TRUE);
		map.put(MSG, msg);
		map.put(RESULT, result);
		return map;
	}

	public static Map<String, Object> falseBizResult(Object result, String msg) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(STATUTS, Boolean.TRUE);
		map.put(MSG, msg);
		map.put(RESULT, result);
		return map;
	}

	public static Map<String, Object> trueBizResult(Object result, String msg, String errorCode) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(STATUTS, Boolean.TRUE);
		map.put(MSG, msg);
		map.put(ERRORCODE, errorCode);
		map.put(RESULT, result);
		return map;
	}

	public static Map<String, Object> falseBizResult(Object result, String msg, String errorCode) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(STATUTS, Boolean.FALSE);
		map.put(MSG, msg);
		map.put(ERRORCODE, errorCode);
		map.put(RESULT, result);
		return map;
	}
}
